<?php 
global $_GPC, $_W;
$shopid=check_right(2);
if(!$shopid)message("请先选择需要管理的店铺",$this->createWebUrl('shop'),'error');
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if($operation=="supplier"){
	$where="";
	if($_GPC['keyword'])$where.=" and (realname like '%".$_GPC['keyword']."%' or mobile like '%".$_GPC['keyword']."%' )";
	$list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_supplier')." WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' $where order by id desc");
	
} elseif ($operation == 'getsupplier') {
	$id=intval($_GPC['id']);
	if($id!=0){
		$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_supplier')." WHERE id = :id ",array(':id'=>$id));
		die(json_encode(array("success"=>true,"item"=>$item)));
	}
	die(json_encode(array("success"=>true,"item"=>"")));
	
} elseif ($operation == 'postsupplier') {
	$id = intval($_GPC['id']);
	$data = array(
		'weid' => $_W['uniacid'], 
		'title' => $_GPC['title'], 
		'groupid' => intval($shopid), 
		'realname' => $_GPC['realname'], 
		'mobile' => $_GPC['mobile'], 
		'createtime' => TIMESTAMP, 
	);
	if (!empty($id)) {
		unset($data['createtime']);
		pdo_update('j_hmoney_supplier', $data, array('id' => $id));
	} else {
		pdo_insert('j_hmoney_supplier', $data);
	}
	die(json_encode(array("success"=>true,"item"=>$item)));
	
} elseif ($operation == 'deletesupplier') {
	$id=intval($_GPC['id']);
	if($id){
		pdo_delete('j_hmoney_supplier',array('id'=>$id));
	}
	message("删除成功", $this->createWebUrl('store',array('op'=>'supplier')), 'success');
	
} elseif ($operation == 'display') {
	$list = pdo_fetchall("SELECT * FROM " . tablename('j_hmoney_store')." WHERE weid = '{$_W['uniacid']}' and groupid ='".$shopid."' order by id desc");
	$supplierlist= pdo_fetchall("SELECT * FROM ".tablename("j_hmoney_supplier")." WHERE weid = '".$_W['uniacid']."' and groupid ='".$shopid."' ORDER BY id asc");
	$supplierary=array();
	foreach($supplierlist as $row){
		$supplierary[$row['id']]=$row['title'];
	}
	$grouplist= pdo_fetchall("SELECT * FROM ".tablename("j_hmoney_group")." WHERE weid = '".$_W['uniacid']."' ORDER BY id asc");
	$groupary=array();
	foreach($grouplist as $row){
		$groupary[$row['id']]=$row['companyname'];
	}
	$goodcategory= pdo_fetchall("SELECT * FROM ".tablename("j_hmoney_goodcategory")." WHERE weid = '".$_W['uniacid']."' and groupid=:a ORDER BY displayorder asc,id desc",array(":a"=>$shopid));
	$goodlist=pdo_fetchall("SELECT * FROM ".tablename("j_hmoney_goods")." WHERE weid = '".$_W['uniacid']."' and pcate=:a ORDER BY displayorder asc,id desc",array(":a"=>$goodcategory[0]["id"]));
	
} elseif ($operation == 'searchgoods') {
	$keyword=trim($_GPC['keyword']);
	if($keyword){
		$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_goods')." WHERE  groupid ='".$shopid."' and goodsn=:a order by id desc limit 1",array(':a'=>$keyword));
		if(!$item)die(json_encode(array("success"=>false)));
		die(json_encode(array("success"=>true,"item"=>$item)));
	}
	die(json_encode(array("success"=>false)));
	
} elseif ($operation == 'getstoreno') {
	$ordersn=$_GPC['ordersn'];
	$order=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_store')." WHERE weid = '{$_W['uniacid']}' and ordersn=:a",array(":a"=>$ordersn));
	if(!$order)die(json_encode(array("success"=>false,"msg"=>"单号不存在")));
	$goodlist=pdo_fetchall("SELECT a.*,b.* FROM ".tablename('j_hmoney_storedetail')." as a left join(select * from ".tablename('j_hmoney_goods').") as b on a.goodid=b.id WHERE a.ordersn='".$ordersn."' order by a.id asc");
	die(json_encode(array("success"=>true,"item"=>$order,"goodlist"=>$goodlist)));
	
} elseif ($operation == 'poststore') {
	$ordersn=$_GPC['ordersn'];
	$data=array(
		'weid' => $_W['uniacid'],  
		'groupid' => intval($shopid),  
		'createtime' => TIMESTAMP, 
		'uid' => intval($uid), 
		'sid' => intval($_GPC['sid']), 
		'totalfee' => floatval($_GPC['totalfee']), 
		'paidfee' => floatval($_GPC['paidfee']), 
		'otherfee' => floatval($_GPC['otherfee']), 
		'paytype' => intval($_GPC['paytype']), 
		'remark' => trim($_GPC['remark']), 
		'status' => 0, 
	);
	if(!$ordersn){
		$data['ordersn']="PUR".TIMESTAMP;
		pdo_insert('j_hmoney_store', $data);
	}else{
		unset($data['createtime']);
		pdo_update('j_hmoney_store', $data, array('ordersn' => $ordersn));
		pdo_delete('j_hmoney_store',array("ordersn"=>$ordersn));
	}
	$sql="insert into ".tablename('j_hmoney_storedetail')." (weid,groupid,ordersn,goodid,num,price,totalfee,paidfee,status,createtime)VALUES";
	$ary=array();
	$num=0;
	foreach($_GPC["num"] as $index=>$row){
		$temp=array($_W['uniacid'],$shopid,$data['ordersn'],$index,$row,$_GPC["price"][$index],$_GPC["total"][$index],$_GPC["paid"][$index],0,TIMESTAMP);
		$ary[]="('".implode("','",$temp)."')";
		$num+=$row;
	}
	$sql=$sql.implode(",",$ary);
	pdo_run($sql);
	pdo_update('j_hmoney_store', array("num"=>$num), array('ordersn' => $data['ordersn']));
	die(json_encode(array("success"=>true)));
	
} elseif ($operation == 'deletestore') {
	$id=intval($_GPC['id']);
	if($id){
		$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_store')." WHERE id = :id ",array(':id'=>$id));
		if($item['status']>0)message("已入库订单不能删除哦", $this->createWebUrl('store',array('op'=>'display')), 'error');
		$ordersn=$item['ordersn'];
		pdo_delete('j_hmoney_store',array('id'=>$id));
		pdo_delete('j_hmoney_storedetail',array('ordersn'=>$ordersn));
	}
	message("删除成功", $this->createWebUrl('store',array('op'=>'display')), 'success');
	
} elseif ($operation == 'instore') {
	$id=intval($_GPC['id']);
	if(!$id)die(json_encode(array("success"=>false,"msg"=>"订单编号不能为空")));
	$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_store')." WHERE id = :id ",array(':id'=>$id));
	if($item['status']>0)die(json_encode(array("success"=>false,"msg"=>"已入库订单不能重复入库")));
	$ordersn=$item['ordersn'];
	$list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_storedetail')." WHERE ordersn = :a ",array(':a'=>$ordersn));
	$ary=array();
	foreach($list as $row){
		$ary[]="update ".tablename('j_hmoney_goods')." set store=store+".$row['num']." where id=".$row['goodid'];
	}
	$sql=implode(";",$ary);
	pdo_run($sql);
	pdo_update("j_hmoney_store",array("status"=>1,"instoretime"=>TIMESTAMP),array("id"=>$id));
	pdo_update("j_hmoney_storedetail",array("status"=>1),array("ordersn"=>$ordersn));
	die(json_encode(array("success"=>true)));
	
} elseif ($operation == 'getpcategood') {
	$pcate=intval($_GPC['pcate']);
	if(!$pcate)die(json_encode(array("success"=>false)));
	$goodlist=pdo_fetchall("SELECT * FROM ".tablename("j_hmoney_goods")." WHERE weid = '".$_W['uniacid']."' and pcate=:a ORDER BY displayorder asc,id desc",array(":a"=>$pcate));
	die(json_encode(array("success"=>true,"list"=>$goodlist)));
	
} elseif ($operation == 'postsalereturn') {
	$ordersn=trim($_GPC['ordersn']);
	$order=pdo_fetch("SELECT * FROM ".tablename("j_hmoney_store")." WHERE weid = '".$_W['uniacid']."' and ordersn=:a ",array(":a"=>$ordersn));
	if($order['status']==2)die(json_encode(array("success"=>false,"msg"=>"订单已全部退货")));
	$outordersn="OUT".TIMESTAMP;
	$sql="insert into ".tablename('j_hmoney_storereturndetail')." (weid,groupid,outordersn,ordersn,goodid,num,price,totalfee,paidfee,status,createtime)VALUES";
	$ary=array();
	$num=0;
	$ary2=array();
	$sql2="update ".tablename('j_hmoney_storedetail')." SET `num` = CASE goodid ";
	$idary=array();
	foreach($_GPC["num"] as $index=>$row){
		if($row==0)continue;
		$temp=array($_W['uniacid'],$shopid,$outordersn,$ordersn,$index,$row,$_GPC["price"][$index],$this->_2money(($_GPC["price"][$index]*$row)*100),$_GPC["paid"][$index],1,TIMESTAMP);
		$ary[]="('".implode("','",$temp)."')";
		$sql2.=" WHEN ".$index." THEN `num`-".$row." ";
		$idary[]=$index;
		$num+=$row;
	}
	$sql2.=" END ";
	$sql2.=" WHERE goodid IN (".implode(',',$idary).") and ordersn='".$ordersn."'";
	if(count($ary)==0)die(json_encode(array("success"=>false,"msg"=>"请选择退货商品")));
	$data=array(
		'weid' => $_W['uniacid'],  
		'groupid' => intval($shopid),
		'createtime' => TIMESTAMP,
		'uid' => intval($uid),
		'num' => intval($num),
		'totalfee' => floatval($_GPC['totalfee']), 
		'paidfee' => floatval($_GPC['paidfee']), 
		'otherfee' => floatval($_GPC['otherfee']), 
		'paytype' => intval($_GPC['paytype']), 
		'remark' => trim($_GPC['remark']), 
		'ordersn' => trim($ordersn),
		'outordersn' => trim($outordersn),
		'status' => 1, 
	);
	pdo_insert('j_hmoney_storereturn', $data);
	$sql=$sql.implode(",",$ary);
	pdo_run($sql);
	pdo_run($sql2);
	$num=pdo_fetchcolumn("SELECT sum(num) FROM ".tablename("j_hmoney_storedetail")." WHERE weid = '".$_W['uniacid']."' and ordersn=:a ",array(":a"=>$ordersn));
	if($num==0){
		pdo_update("j_hmoney_store",array("status"=>2,"num"=>0),array("ordersn"=>$ordersn));
	}else{
		pdo_update("j_hmoney_store",array("num"=>$num),array("ordersn"=>$ordersn));
	}
	/*扣减商品库存*/
	if($order['status']==1){
		$sql2="update ".tablename('j_hmoney_goods')." SET `store` = CASE id ";
		$idary=array();
		foreach($_GPC["num"] as $index=>$row){
			if($row==0)continue;
			$sql2.=" WHEN ".$index." THEN `store`-".$row." ";
			$idary[]=$index;
		}
		$sql2.=" END ";
		$sql2.=" WHERE id IN (".implode(',',$idary).") ";
		pdo_run($sql2);
	}
	die(json_encode(array("success"=>true)));
	
} elseif ($operation == 'storeview') {
	$ordersn=$_GPC["ordersn"];
	$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_store')." WHERE ordersn=:a ",array(':a'=>$ordersn));
	$goodlist=pdo_fetchall("SELECT a.*,b.* FROM ".tablename('j_hmoney_storedetail')." as a left join(select * from ".tablename('j_hmoney_goods').") as b on a.goodid=b.id WHERE a.ordersn='".$ordersn."' order by a.id asc");
	$goodary=array();
	foreach($goodlist as $row){
		$goodary[$row['goodid']]=$row;
	}
	$salereturn=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_storereturn')." WHERE ordersn=:a order by id asc",array(':a'=>$ordersn));
	$salegoodlist=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_storereturndetail')." WHERE ordersn=:a order by id asc",array(':a'=>$ordersn));
	$salegoodary=array();
	foreach($salegoodlist as $row){
		if(!isset($salegoodary[$row['outordersn']]))$salegoodary[$row['outordersn']]=array();
		$salegoodary[$row['outordersn']][]=$row;
	}
	include $this->template('web/store/store_tpl');
	die();
	
} elseif ($operation == 'checkstock') {
	
} elseif ($operation == 'getgoodstore') {
	if(!$_GPC['num'])die(json_encode(array("success"=>false,"msg"=>"请先录入商品.")));
	$keys=array_keys($_GPC['num']);
	if(!count($keys))die(json_encode(array("success"=>false,"msg"=>"请先录入商品")));
	$list=pdo_fetchall("SELECT id,store FROM ".tablename('j_hmoney_goods')." WHERE weid = '{$_W['uniacid']}' and id in(".implode(",",$keys).") order by id desc");
	die(json_encode(array("success"=>true,"list"=>$list)));
	
}
include $this->template('web/'.$cfg['floder'].'/store');






